load("@rules_cc//cc:defs.bzl", "cc_test")

_TESTS = {
    "test_headers/test": [
        "classic_actions", "classic_actor", "classic_alternative", "classic_as_parser", "classic_assert",
        "classic_assign_actor", "classic_assign_key_actor", 
        # Fails to build "classic_ast_fwd",
        "classic_ast", 
        "classic_attribute", "classic_basic_chset", "classic_chset", "classic_clear_actor", 
        "classic_closure_context", "classic_closure_fwd", "classic_common_fwd", "classic_common",
        "classic_composite", "classic_config", "classic_confix_fwd", "classic_confix", "classic_core",
        # Fails to build "classic_debug_node",
        "classic_debug", "classic_decrement_actor", "classic_difference",
        "classic_directives", 
        # Fails to build "classic_distinct_fwd",
        "classic_distinct", "classic_dynamic", 
        "classic_epsilon", "classic_erase_actor", "classic_escape_char_fwd", "classic_escape_char", 
        "classic_exceptions_fwd", "classic_exceptions", "classic_exclusive_or", "classic_file_iterator_fwd",
        "classic_file_iterator", "classic_fixed_size_queue", "classic_flush_multi_pass", "classic_for",
        "classic_functor_parser", 
        # Fails to build "classic_fundamental",
        # Fails to build "classic_grammar_def_fwd", 
        # Fails to build "classic_grammer_def",
        "classic_if", "classic_increment_actor", "classic_insert_at_actor", "classic_insert_key_actor",
        "classic_intersection", "classic_iterator", "classic_kleene_star", "classic_lazy", 
        # Fails to build "classic_list",
        # Fails to build "classic_lists", 
        "classic_loops", "classic_match", 
        # Fails to build "classic_meta", 
        # Fails to build "classic_minimal", 
        "classic_multi_pass_fwd",
        "classic_multi_pass", "classic_nil", "classic_no_actions", "classic_numerics_fwd",
        "classic_numerics", "classic_operators", "classic_optional", "classic_parametric", 
        # Fails to build "classic_parse_tree_fwd",
        "classic_parse_tree_utils", "classic_parse_tree", 
        # Fails to build "classic_parse_context", 
        # Fails to build "classic_parser_id", 
        "classic_parser_names", "classic_parser_traits", "classic_parser", "classic_position_iterator_fwd", 
        "classic_position_iterator", "classic_positive", "classic_primitives", "classic_push_back_actor", 
        "classic_push_front_actor", "classic_range_run", "classic_ref_actor", "classic_ref_const_ref_actor", 
        "classic_ref_const_ref_const_ref_a", "classic_ref_const_ref_value_actor", "classic_ref_value_actor", 
        "classic_refactoring", "classic_regex", "classic_rule_alias", "classic_rule_parser", "classic_rule",
        "classic_safe_bool", "classic_scanner_fwd", "classic_scanner", 
        # Fails to build "classic_scoped_lock", 
        "classic_select",
        "classic_sequence", 
        # Fails to build "classic_sequential_and", 
        "classic_sequential_or", "classic_skipper_fwd", 
        # Fails to build "classic_skipper",
        "classic_spirit", "classic_stored_rule_fwd", "classic_stored_rule",
        "classic_subrule_fwd", "classic_subrule", "classic_swap_actor", "classic_switch", "classic_symbols_fwd",
        "classic_symbols", 
        # Fails to build "classic_traverse", 
        # Fails to build "classic_tree_to_xml",
        "classic_typeof", "classic_utility", 
        "classic_version", "classic_while", "classic", "phoenix1_actor", "phoenix1_binders", "phoenix1_casts", 
        "phoenix1_closures", "phoenix1_composite",
        # Fails to build "phoneix1_functions", 
        "phoenix1_new", "phoenix1_operators",
        "phoenix1_primitives", 
        # Fails to build "phoenix1_special_ops",
        "phoenix1_statements", "phoenix1_tuple_helpers", "phoenix1_tuples",
        "phoenix1"
    ],
}

_LINUX_TESTS = {
    "test_headers/test": ["classic_static"]
}

_MAC_TESTS = {
    "test_headers/test": ["classic_static"]
}

[[cc_test(
    name = "{}_{}".format(test.replace("/", "_"), header),
    srcs = ["{}.cpp".format(test)],
    defines = ["BOOST_SPIRIT_HEADER_NAME=boost/spirit/include/{}.hpp".format(header)] + select({
        "@platforms//os:windows": ["WIN32_LEAN_AND_MEAN=1"],
        "@platforms//os:macos": [],
        "//conditions:default": [],
    }),
    deps = [
        "@boost.test",
        "@boost.test//:unit_test_main",
        "@boost.spirit",
    ],
    linkstatic = select({
        # Windows needs to link statically to get rid of undefined symbols
        "@platforms//os:windows": True,
        "@platforms//os:macos": False,
        # Posix needs to link statically to get rid of undefined symbols
        "//conditions:default": True,
    }),
) for header in _TESTS[test]]for test in _TESTS]

[[cc_test(
    name = "{}_{}_linux".format(test.replace("/", "_"), header),
    srcs = ["{}.cpp".format(test)],
    defines = ["BOOST_SPIRIT_HEADER_NAME=boost/spirit/include/{}.hpp".format(header)] + select({
        "@platforms//os:windows": ["WIN32_LEAN_AND_MEAN=1"],
        "@platforms//os:macos": [],
        "//conditions:default": [],
    }),
    target_compatible_with = select({
        "@platforms//os:windows": ["@platforms//:incompatible"],
        "@platforms//os:macos": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
    deps = [
        "@boost.test",
        "@boost.test//:unit_test_main",
        "@boost.spirit",
    ],
    # Posix needs to link statically to get rid of undefined symbols
    linkstatic = True,
) for header in _LINUX_TESTS[test]]for test in _LINUX_TESTS]

[[cc_test(
    name = "{}_{}_mac".format(test.replace("/", "_"), header),
    srcs = ["{}.cpp".format(test)],
    defines = ["BOOST_SPIRIT_HEADER_NAME=boost/spirit/include/{}.hpp".format(header)] + select({
        "@platforms//os:windows": ["WIN32_LEAN_AND_MEAN=1"],
        "@platforms//os:macos": [],
        "//conditions:default": [],
    }),
    target_compatible_with = select({
        "@platforms//os:windows": ["@platforms//:incompatible"],
        "@platforms//os:macos": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    deps = [
        "@boost.test",
        "@boost.test//:unit_test_main",
        "@boost.spirit",
    ],
    linkstatic = False,
) for header in _MAC_TESTS[test]]for test in _MAC_TESTS]
